import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:oss_dart/oss_dart.dart';

void main() {
  group('OssClient V2 Tests', () {
    test('should create OssClient with STS authentication', () {
      final client = OssClient.sts(
        bucketName: 'test-bucket',
        endpoint: 'oss-cn-hangzhou.aliyuncs.com',
        tokenGetter: () async => {
          'AccessKeyId': 'test-key',
          'AccessKeySecret': 'test-secret',
          'SecurityToken': 'test-token',
          'Expiration': '2024-12-31T23:59:59Z',
        },
      );

      expect(client.bucketName, equals('test-bucket'));
      expect(client.endpoint, equals('oss-cn-hangzhou.aliyuncs.com'));
      expect(client.tokenGetter, isNotNull);
    });

    test('should create OssClient with direct authentication', () {
      final client = OssClient.direct(
        bucketName: 'test-bucket',
        endpoint: 'oss-cn-hangzhou.aliyuncs.com',
        directAccessKey: 'test-access-key',
        directAccessSecret: 'test-access-secret',
      );

      expect(client.bucketName, equals('test-bucket'));
      expect(client.endpoint, equals('oss-cn-hangzhou.aliyuncs.com'));
      expect(client.directAccessKey, equals('test-access-key'));
      expect(client.directAccessSecret, equals('test-access-secret'));
    });

    test('should create OssClient with legacy constructor (STS mode)', () {
      final client = OssClient(
        bucketName: 'test-bucket',
        endpoint: 'oss-cn-hangzhou.aliyuncs.com',
        tokenGetter: () async => {
          'AccessKeyId': 'test-key',
          'AccessKeySecret': 'test-secret',
          'SecurityToken': 'test-token',
          'Expiration': '2024-12-31T23:59:59Z',
        },
      );

      expect(client.bucketName, equals('test-bucket'));
      expect(client.endpoint, equals('oss-cn-hangzhou.aliyuncs.com'));
    });

    test('should validate content type detection', () {
      expect(contentTypeByFilename('test.jpg'), equals('image/jpeg'));
      expect(contentTypeByFilename('test.png'), equals('image/png'));
      expect(contentTypeByFilename('test.pdf'), equals('application/pdf'));
      expect(contentTypeByFilename('test.unknown'), equals('application/octet-stream'));
      expect(contentTypeByFilename('test'), equals('application/octet-stream'));
    });

    test('should validate MD5 generation', () {
      final List<int> testData = [1, 2, 3, 4, 5];
      final String md5Hash = md5File(testData);

      expect(md5Hash, isNotEmpty);
      expect(md5Hash, isA<String>());
    });

    test('should validate HMAC signature generation', () {
      const String secret = 'test-secret';
      const String raw = 'test-string';
      final String signature = hmacSign(secret, raw);

      expect(signature, isNotEmpty);
      expect(signature, isA<String>());
    });

    test('should generate HTTP date format', () {
      final String httpDate = httpDateNow();

      expect(httpDate, isNotEmpty);
      expect(httpDate, endsWith(' GMT'));
      expect(httpDate, isA<String>());
    });

    test('should have objectExists method', () {
      final client = OssClient.direct(
        bucketName: 'test-bucket',
        endpoint: 'oss-cn-hangzhou.aliyuncs.com',
        directAccessKey: 'test-access-key',
        directAccessSecret: 'test-access-secret',
      );

      // 验证方法存在且返回Future<bool>
      expect(client.objectExists('test-file.txt'), isA<Future<bool>>());
    });

    test('should have getObjectMeta method', () {
      final client = OssClient.sts(
        bucketName: 'test-bucket',
        endpoint: 'oss-cn-hangzhou.aliyuncs.com',
        tokenGetter: () async => {
          'AccessKeyId': 'test-key',
          'AccessKeySecret': 'test-secret',
          'SecurityToken': 'test-token',
          'Expiration': '2024-12-31T23:59:59Z',
        },
      );

      // 验证方法存在且返回Future<http.Response>
      expect(client.getObjectMeta('test-file.txt'), isA<Future<http.Response>>());
    });
  });
}
